package com.qf.service.Impl;

import com.qf.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


@Service

public class MailServiceImpl implements MailService {



    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Override
    @Async
    public void sendSimpleMail(String to, String subject, String content) throws MessagingException {

//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        //创建邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //设置收件人 发件人 主体
        MimeMessageHelper messageHelper=new MimeMessageHelper(mimeMessage,true);
        messageHelper.addTo(to);
        messageHelper.setFrom(from);
        messageHelper.setSubject(subject);
        messageHelper.setText("这个是纯文本部分",content );

        javaMailSender.send(messageHelper.getMimeMessage());


        //发送邮件
    }
}
